package com.itheima.itheima_health.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.itheima_health.constant.MessageConstant;
import com.itheima.itheima_health.entity.PageResult;
import com.itheima.itheima_health.entity.QueryPageBean;
import com.itheima.itheima_health.exception.BaseException;
import com.itheima.itheima_health.mapper.CheckitGroupMapper;
import com.itheima.itheima_health.pojo.CheckGroup;
import com.itheima.itheima_health.service.CheckitemGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CheckitemGroupServiceImpl implements CheckitemGroupService {
    @Autowired
    private CheckitGroupMapper checkitGroupMapper;
    @Override
    public PageResult CheckitemGropPageQuery(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //查询相应分页数据
        Page<CheckGroup> page = checkitGroupMapper.CheckitemGropPageQuery(queryPageBean);

        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    @Transactional
    public void CheckitemGroupAdd(CheckGroup checkGroup, Integer[] checkitemIds) {

        //添加检查组数据  xml获取生成id
        checkitGroupMapper.CheckitemGroupAdd(checkGroup);

        if (checkitemIds!=null && checkitemIds.length >0){

            //添加检查组、检查项维护表数据
            Integer checkGroupId = checkGroup.getId();
            for (Integer checkitemId : checkitemIds) {
                checkitGroupMapper.CheckItemAndGroupAdd(checkGroupId,checkitemId);
            }
        }
    }

    @Override
    public CheckGroup CheckitemGroupfindById(Long id) {
        CheckGroup checkGroup = checkitGroupMapper.CheckitemGroupfindById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckitemgroupIdBycheckitemId(Long id) {
        List<Integer> checkitemIds = checkitGroupMapper.findCheckitemgroupIdBycheckitemId(id);
        return checkitemIds;
    }
    @Transactional
    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
        //删除该检查组检查项数据
        checkitGroupMapper.deleteCheckGroupOftheCheckitem(checkGroup);

        //基于原有方法 新增该检查组的检查项
        Integer checkGroupId = checkGroup.getId();

        for (Integer checkitemId : checkitemIds) {
            checkitGroupMapper.CheckItemAndGroupAdd(checkGroupId,checkitemId);
        }
        //编辑检查组数据
        checkitGroupMapper.update(checkGroup);
    }

    @Override
    public void delete(Integer checkGroupId) {
        //类型强转
        Long id = Long.valueOf(checkGroupId);
        //查询该检查组是否有检查项 有则不可删除
        List<Integer> list = checkitGroupMapper.findCheckitemgroupIdBycheckitemId(id);
        if (list.size()>0&&list!=null){
            throw new BaseException(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }else {
            checkitGroupMapper.delete(checkGroupId);
        }
    }

    @Override
    public List<CheckGroup> findAllCheckGroup() {
        List<CheckGroup> checkGroupList = checkitGroupMapper.findAllCheckGroup();
        return checkGroupList;
    }
}
